<?php
namespace App\Http\Controllers\Admin\Qudao;

use App\Service\Admin\Qudao\BannerService;
use App\Utilitys\Upload;
use Illuminate\Http\Request;

class BannerController extends BaseController
{
    //轮播图

    public function __construct(BannerService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex()
    {
        $res = $this->db->getList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //上传
    public function add(Request $request)
    {
        $pas['type'] = $request->post('type', 0);
        $pas['img'] = $request->post('img', '');
        if (!$pas['img']) {
            return $this->toJson(-2, '图片必须的');
        }
        $sizes = getimagesize($pas['img']);
        $res = $this->db->add($pas, $sizes[0], $sizes[1]);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }
}